/*
 jChecs: a Java chess game sample 

 Copyright (C) 2006-2011 by David Cotton

 This program is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
 Foundation; either version 2 of the License, or (at your option) any later
 version.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package fr.free.jchecs.swg;

// Silva
import java.util.Scanner;
import java.util.regex.*;
import java.io.*;


/**
 * Lance l'interface Swing du jeu d'échecs.
 * 
 * @author David Cotton
 */
public final class Start {
	/**
	 * Classe utilitaire : ne pas instancier !
	 */
	private Start() {
		// Rien de spécifique...
	}

	/**
	 * Méthode de démarrage.
	 * 
	 * @param pArgs
	 *            Arguments de la ligne de commande : ignorés, aucun argument
	 *            attendu.
	 * @throws InterruptedException 
	 */
	public static void main(final String[] pArgs) throws InterruptedException {
		final ExceptionHandler gestionErreur = new ExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(gestionErreur);

		/* Silva */
		String [] alg = {"MiniMax", "AlphaBeta", "NegaScout", "Random"};
		String [] ply = {"1","2","3","4"};
		String _white = "whites";
		String _black = "blacks";

		
		
		// Execute the tests
		for (int i=1; i <= 4; i++) {
			
			// Update data white algorith depth (this is the one we're studying!
			updateConfig(_white, "AlphaBeta", Integer.toString(i));
			
			for (int pAlg = 0; pAlg < alg.length; pAlg++)	{
				for (int pDepth = 0; pDepth < ply.length; pDepth++) {
					
					// Update config file 
					updateConfig(_black, alg[pAlg], ply[pDepth]);
					
					// Run set of tests 
					UI ihm = new SwingUI();
					gestionErreur.setUI(ihm);
					ihm.start();
					
					// Test iteration complete.  Start over.
					Thread.sleep(2000);
					ihm = null;
				}
			}
		}
		
		// Done. Exit.
		System.out.println("Done!");
		System.exit(0);
	}
	
	static void updateConfig (String color, String alg, String ply) {
		
		String _alg = "player."+ color +".name=";
		String _depth = "player."+ color + ".depth=";
		
		/* Open configuration file */
		File file = new File("jChecs.config");
		Scanner fscan = null;
		
		
		try {
			fscan = new Scanner(file);
			//out = new PrintWriter(file);
		} catch (FileNotFoundException ex){
			ex.printStackTrace();
		}
		
		/* Read in entire file */
		StringBuilder fin = new StringBuilder();
		while (fscan.hasNextLine()){
			fin.append(fscan.nextLine() + "\n");
		}
		
		/* Close the input file */
		fscan.close();
		
		/* Open the file for output */
		PrintWriter fout = null;
		try {
			fout = new PrintWriter(file);
		} catch (FileNotFoundException ex){
			ex.printStackTrace();
		}
		
		// Update the algorithm
		fin.replace(0,  fin.length(), fin.toString().replaceFirst(_alg+".*", _alg+alg));
		
		// Update search depth
		fin.replace(0,  fin.length(), fin.toString().replaceFirst(_depth+".*", _depth+ply));
		
		// Write updated setting to the file
		fout.print(fin.toString());
		
		fout.close();
		
		
		
	}
	
}
